<?php 
## include required files
/*******************************/
require_once '../model/country.php';
require_once '../model/state.php';
require_once '../model/city.php';
/*******************************/
## Create Objects
/*******************************/
$countryObj = new Model_Country();
$stateObj=new Model_State();
$cityObj=new Model_City();
/*******************************/

if(isset($_POST['editcity_btn']) || $_POST['cityname']) {	
	## apply PHP validation for required filed
	if(trim($_POST['state_id']) != '') {
		if(trim($_POST['cityname']) != '' ) {
		extract($_POST);		
		$cityArray['country_id']    = 226;
		$cityArray['city_name']     = return_post_value($cityname);				
		$cityArray['state_id']      = return_post_value($state_id);
		$cityObj->editCityById($cityArray,$city_id);
		$_SESSION['msg'] = "<div class='success_msg'><span>City edited successfully!</span></div>";
		header('location:'.SITE_URL.'/admin/home.php?q=city&page='.$_GET['page']);
		exit;
		} else {// php validation else
		$_SESSION['msg'] = "<div class='error_msg'><span>Please enter city name</span></div>";
	}
	} else {// php validation else
		$_SESSION['msg'] = "<div class='error_msg'>Please fill all required fields</span></div>";
	}
	// main if closed
}

if($_GET['id']!='')
{
	$city_id=base64_decode($_GET['id']);
	$cityArr=$cityObj->getCityDetailsById($city_id);
	$smarty->assign('cityArr', $cityArr);
}
## fetching all the country
$countryArray=$countryObj->getAllCountry();
$smarty->assign('countryArray', $countryArray);

##fetching all the state
if($cityArr['country_id']) {
	$stateArray = $stateObj->getAllStateByCountryId($cityArr['country_id']);
	$smarty->assign('stateArray', $stateArray);
}

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'city');
$smarty->assign('mainmenu', '2');
## Unset all the objects created which are on this page
unset($countryObj);
unset($stateObj);
unset($cityObj);


$smarty->display(TEMPLATEDIR_ADMIN. 'city/editcity.tpl');
unset($smarty);
?>